##############################################################################
##
##  wizard/addcolumn.py
##
##  The GUI component of the addcolumn wizard.
##
##############################################################################

#import copy
#import columns
#from command import gui, doc
#from _wizard import Wizard
#from _page import Page
from PyQt4.QtGui import *

_id_neworexisting = 1
_id_new = 2
_id_existing = 3

class pageNewOrExisting(QWizardPage):

   def initializePage(self):
      self.setLayout(QVBoxLayout())
      self.setTitle('New or Existing')
      self.setSubTitle('Select whether you want to add a new or existing column to the schedule')
      radioNew = QRadioButton(self, 'Create a new column')
      radioExisting = QRadioButton(self, 'Add an existing column')
      radioNew.setValue(True)
      self.registerField('isnew', radioNew, 'value')
      #self.setFinalPage(True)
   #def nextId(self):
   #   return _id_new if self.field('isnew') else _id_existing
    
   
class Wiz(QWizard):
   def __init__(self):
      QWizard.__init__(self, qApp.mainwindow)
      self.coltype = None
      
   def __call__(self):
      # Set up pages
      self.setWindowTitle('Add column')
      self.addPage(pageNewOrExisting())
#      self.pgExist = Page(self, 'Existing Column')
#      self.pgNew = Page(self, "Column Name and Type")
#      self.pgLinks = Page(self, "Link to Columns")
#      self.pgSch = Page(self, "Add to Schedules")
#      self.pages[0] = self.pgNOrE

#      # Prepare data for pages
#      self.existcols = [ col.name for col in doc().columns ]
#      schedules = [ sch.name for sch in doc().schedules ]
#      coltypes = columns.all
#
#      # Create widgets
#      self.optnew = wx.RadioButton(self.pgNOrE, label='New Column', style=wx.RB_GROUP)
#      self.optexist = wx.RadioButton(self.pgNOrE, label='Existing Column')
#      self.lstexistcols = wx.ListBox(self.pgExist, choices=self.existcols)
#      self.txtname = wx.TextCtrl(self.pgNew)
#      self.lstcoltypes = wx.ListBox(self.pgNew, choices=coltypes)
#      self.cmblinks = {}
#      self.lstschedules = wx.ListBox(self.pgSch, choices=schedules, style=wx.LB_MULTIPLE)
#
#      # Page 0 - New or existing
#      self.pgNOrE.AddText('Use an existing column or create a new one?')
#      self.pgNOrE.AddWidget(self.optexist)
#      self.pgNOrE.AddWidget(self.optnew)
#      self.pgNOrE.SetNext(self.pgNew)
#
#      # Page 1 - Select existing
#      self.pgExist.SetPrev(self.pgNOrE)
#      self.pgExist.AddText('Select the existing column to add')
#      self.pgExist.AddWidget(self.lstexistcols)
#      self.pgExist.SetNext(self.pgSch)
#      
#      # Page 2 - Name and type of new
#      self.pgNew.SetPrev(self.pgNOrE)
#      self.pgNew.AddText('Enter the name and type of the column')
#      self.pgNew.AddWidget(self.txtname)
#      self.pgNew.AddWidget(self.lstcoltypes)
#      self.pgNew.SetNext(self.pgLinks)
#      
#      # Page 3 - Links
#      self.pgLinks.SetPrev(self.pgNew)
#      self.pgLinks.SetNext(self.pgSch)
#
#      # Page 4 - Add to schedules
#      self.pgSch.SetPrev(self.pgLinks)
#      self.pgSch.AddText("Select the schedules to add this column to")
#      self.pgSch.AddWidget(self.lstschedules)
#
#      # Set defaults and bind events
#      self.lstcoltypes.Bind(wx.EVT_LISTBOX, self.OnOptionChange)
#      self.optnew.Bind(wx.EVT_RADIOBUTTON, self.OnOptionChange)
#      self.optexist.Bind(wx.EVT_RADIOBUTTON, self.OnOptionChange)
#      self.optnew.SetValue(True)
#      self.lstcoltypes.SetSelection(0)
#      self.OnOptionChange(None)
#
#      # Run the wizard
#      return self.run()

   def OnPageChange(self, prev, next):
      if prev is self.pgNew and next is self.pgLinks:
         self.pgLinks.Clear()
         for link in self.coltype.required_links:
            self.cmblinks[link] = wx.ComboBox(self.pgLinks, choices=existing_cols, style=wx.CB_READONLY)
            self.cmblinks[link].SetItem[0]
            self.pgLinks.AddText("Select the column to use for " + link)
            self.pgLinks.AddWidget(self.cmblinks[link])
            
      elif next is self.pgSch:
         if not self.newcol:
            for i in range(doc().schedules):
               if _getexistcol(self) in doc().schedules[i].columns:
                  self.lstsch.Select(i)

      return True
      
   def OnOptionChange(self,event):
      self.newcol = self.optnew.GetValue()
      if self.newcol:
         self.pgNOrE.SetNext(self.pgNew)
         self.pgNew.SetNext(self.pgSch)
         self.pgSch.SetPrev(self.pgNew)
         self.coltype = columns.__dict__[self.lstcoltypes.GetStringSelection()]
            
         if len(self.coltype.required_links) != 0:
            if len(self.existcols) == 0:
               Error('There must be at least one existing column for this.', show = True)
               self.lstcoltypes.SetSelection(0)
            else:
               self.pgNew.SetNext(self.pgLinks)
               self.pgSch.SetPrev(self.pgLinks)
      else:
         self.pgNOrE.SetNext(self.pgExist)
         self.pgSch.SetPrev(self.pgExist)
            
      
   def _getexistcol(self):
      return doc().columns[self.lstexistcols.GetSelections()[0]] if not self.newcol else None

   def complete(self):
      _c.beginCommand('Add Column to Schedules')

      # Get dict of linked column
      linked = {}
      for cmblink in self.cmblinks.items():
         linked[cmblink[0]] = doc().columns[cmblink[1].GetCurrentSelection()]
      # Get list of schedules to add the column to
      schedules = [ doc().schedules[sch] for sch in self.lstschedules.GetSelections() ]
      # Get column
      if self.newcol:
         args = (self.txtname.GetValue(), doc().master)
         if len(linked) > 0: args = args + tuple(linked)
         column = self.coltype(*args)
         doc().columns.append(column)
      else:
         column = _getexistcol(self)
      
      for schedule in doc().schedules:
         if schedule in schedules:
            schedule.columns.append(column)
         else:
            schedule.columns.remove(column)
      _c.endCommand()
